package com.example.transit.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;


public class DbContentProvider extends ContentProvider {

    private static final String PROVIDER_NAME = "com.example.transit.contentprovider";

    public static final Uri CONTENT_URI_ALLSTATIONINFO = Uri.parse("content://" + PROVIDER_NAME + "/allStaInfo");
    public static final Uri CONTENT_URI_HISTORYINFO = Uri.parse("content://" + PROVIDER_NAME + "/historyinfo");


    private DbHelper mDbHelper;
    private SQLiteDatabase mDatabase;

    private static final int ALLSTAINFO = 1;
    //历史记录
    private static final int HISTORYINFO = 2;

    private static final UriMatcher mUriMatcher;

    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(PROVIDER_NAME, "allStaInfo", ALLSTAINFO);
        mUriMatcher.addURI(PROVIDER_NAME, "historyinfo", HISTORYINFO);
    }


    private String UriToTable(Uri uri) {
        String table = null;
        switch (mUriMatcher.match(uri)) {
            case ALLSTAINFO:
                table = DbHelper.TABLE_ALLSTATION;
                break;
            case HISTORYINFO:
                table = DbHelper.TABLE_HISTORY;
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        return table;
    }


    @Override
    public boolean onCreate() {
        mDbHelper = new DbHelper(getContext());
        mDatabase = mDbHelper.getReadableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        String table = UriToTable(uri);
        qb.setTables(table);
        Cursor cursor = qb.query(mDatabase, projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;

    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        String table = UriToTable(uri);
        mDatabase.insert(table, null, values);
        return null;
    }

    //删除表格
    public void deleteTableData() {
        mDatabase.execSQL(DbHelper.SQL_DELETE_TABLE_ALLSTATION_DATA);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {

        String table = UriToTable(uri);
        return mDatabase.delete(table, selection, selectionArgs);

    }


    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {

        String table = UriToTable(uri);
        return mDatabase.update(table, values, selection, selectionArgs);

    }

}